home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / DemoDialogs / UDemoDialogs.More.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  35.6 KB  |  1,282 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UDemoDialogs.More.cp 
  3. // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UDEMODIALOGS__
  7. #include "UDemoDialogs.h"
  8. #endif
  9.  
  10. // DemoDialogs
  11.  
  12. #ifndef __USTRUCTUREINSPECTORS__
  13. #include "UStructureInspectors.h"
  14. #endif
  15.  
  16. // MacApp
  17.  
  18. #ifndef __UCONTROL__
  19. #include "UControl.h"
  20. #endif
  21.  
  22. #ifndef __UDRAWINGENVIRONMENT__
  23. #include "UDrawingEnvironment.h"
  24. #endif
  25.  
  26. #ifndef __UFAILURE__
  27. #include "UFailure.h"
  28. #endif
  29.  
  30. #ifndef __UGEOMETRY__
  31. #include "UGeometry.h"
  32. #endif
  33.  
  34. #ifndef __UKEYSELECTIONBEHAVIOR__
  35. #include "UKeySelectionBehavior.h"
  36. #endif
  37.  
  38. #ifndef __UMACAPPUTILITIES__
  39. #include "UMacAppUtilities.h"
  40. #endif
  41.  
  42. #ifndef __UMEMORY__
  43. #include "UMemory.h"
  44. #endif
  45.  
  46. #ifndef __UMENUMGR__
  47. #include "UMenuMgr.h"
  48. #endif
  49.  
  50. #ifndef __UPOPUP__
  51. #include "UPopup.h"
  52. #endif
  53.  
  54. #ifndef __UPRINTING__
  55. #include "UPrinting.h"
  56. #endif
  57.  
  58. #ifndef __USCROLLER__
  59. #include "UScroller.h"
  60. #endif
  61.  
  62. #ifndef __UTABBEHAVIORS__
  63. #include "UTabBehaviors.h"
  64. #endif
  65.  
  66. #ifndef __UTEVIEW__
  67. #include "UTEView.h"
  68. #endif
  69.  
  70. #ifndef __UVIEWSERVER__
  71. #include "UViewServer.h"
  72. #endif
  73.  
  74. #ifndef __UWINDOW__
  75. #include "UWindow.h"
  76. #endif
  77.  
  78. // Toolbox
  79.  
  80. #ifndef __FONTS__
  81. #include <Fonts.h>
  82. #endif
  83.  
  84. #ifndef __FP__
  85. #include <fp.h>
  86. #endif
  87.  
  88. #ifndef __MENUS__
  89. #include <Menus.h>
  90. #endif
  91.  
  92. #ifndef __OSUTILS__
  93. #include <OSUtils.h>
  94. #endif
  95.  
  96. #ifndef __PACKAGES__
  97. #include <Packages.h>
  98. #endif
  99.  
  100. #ifndef __RESOURCES__
  101. #include <Resources.h>
  102. #endif
  103.  
  104. #ifndef __TOOLUTILS__
  105. #include <ToolUtils.h>
  106. #endif
  107.  
  108. // ANSI
  109.  
  110. #ifndef __LIMITS__
  111. #include <limits.h>
  112. #endif
  113.  
  114. #ifndef __STDIO__
  115. #include <stdio.h>
  116. #endif
  117.  
  118. #ifndef __STDLIB__
  119. #include <stdlib.h>
  120. #endif
  121.  
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // Constants
  125.  
  126. const short kMaxPlaces = 13;
  127.  
  128. const short mUpArrow = 100;
  129. const short mDownArrow = 101;
  130. const short mTemperatureChanged = 102;
  131.  
  132. //========================================================================================
  133. // CLASS TSwallowBehavior
  134. //========================================================================================
  135. #undef Inherited
  136. #define Inherited TBehavior
  137.  
  138. #pragma segment ARes
  139. MA_DEFINE_CLASS_M1(TSwallowBehavior, Inherited);
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // TSwallowBehavior destructor
  143. //----------------------------------------------------------------------------------------
  144. #pragma segment MADestructorRes
  145.  
  146. TSwallowBehavior::~TSwallowBehavior()
  147. {
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // TSwallowBehavior::DoToolboxEvent: 
  152. //----------------------------------------------------------------------------------------
  153. #pragma segment ARes
  154.  
  155. Boolean TSwallowBehavior::DoToolboxEvent(TToolboxEvent* event)
  156. {
  157.     if (event && (event->fIdentifier == mouseDown) && event->IsOptionKeyPressed())
  158.     {
  159.         gApplication->Beep(1);
  160.         return TRUE;                // consume the event
  161.     }
  162.     else
  163.         return Inherited::DoToolboxEvent(event);
  164. } // TSwallowBehavior::DoToolboxEvent 
  165.  
  166.  
  167.  
  168.  
  169. //========================================================================================
  170. // CLASS TChangeBehavior
  171. //========================================================================================
  172. #undef Inherited
  173. #define Inherited TBehavior
  174.  
  175. #pragma segment ARes
  176. MA_DEFINE_CLASS_M1(TChangeBehavior, Inherited);
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // TChangeBehavior destructor
  180. //----------------------------------------------------------------------------------------
  181. #pragma segment MADestructorRes
  182.  
  183. TChangeBehavior::~TChangeBehavior()
  184. {
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. // TChangeBehavior::DoEvent: 
  189. //----------------------------------------------------------------------------------------
  190. #pragma segment ARes
  191.  
  192. void TChangeBehavior::DoEvent(EventNumber    eventNumber,
  193.                                      TEventHandler*    source,
  194.                                      TEvent*        event)// override 
  195. {
  196.     fOwner->Changed(eventNumber, event);
  197.     Inherited::DoEvent(eventNumber, source, event);
  198. } // TChangeBehavior::DoEvent 
  199.  
  200.  
  201.  
  202. //========================================================================================
  203. // CLASS TUpdateBehavior
  204. //========================================================================================
  205. #undef Inherited
  206. #define Inherited TBehavior
  207.  
  208. #pragma segment ARes
  209. MA_DEFINE_CLASS_M1(TUpdateBehavior, Inherited);
  210.  
  211. //----------------------------------------------------------------------------------------
  212. // TUpdateBehavior destructor
  213. //----------------------------------------------------------------------------------------
  214. #pragma segment MADestructorRes
  215.  
  216. TUpdateBehavior::~TUpdateBehavior()
  217. {
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. // TUpdateBehavior::DoBehaviorUpdate: 
  222. //----------------------------------------------------------------------------------------
  223. #pragma segment ARes
  224.  
  225. void TUpdateBehavior::DoBehaviorUpdate(ChangeID theChange,
  226.                                               TObject* changedObject,
  227.                                               TObject* changedBy,
  228.                                               TDependencySpace* dependencySpace) // override
  229. {
  230.     if (((theChange == mVScrollBarHit) || (theChange == mHScrollBarHit)) && (changedObject != fOwner))
  231.     {
  232.         ((TScrollBar *)(fOwner))->SetLongVal(-((TScrollBar *)(changedObject))->GetLongVal(), kRedraw);
  233.     }
  234.     else
  235.         Inherited::DoBehaviorUpdate(theChange, changedObject, changedBy, dependencySpace);
  236. } // TUpdateBehavior::DoBehaviorUpdate 
  237.  
  238.  
  239. //========================================================================================
  240. // CLASS TArrowsControl
  241. //========================================================================================
  242. #undef Inherited
  243. #define Inherited TPicture
  244.  
  245. #pragma segment AOpen
  246. MA_DEFINE_CLASS_M1(TArrowsControl, Inherited);
  247.  
  248. //----------------------------------------------------------------------------------------
  249. // TArrowsControl constructor 
  250. //----------------------------------------------------------------------------------------
  251. #pragma segment AOpen
  252.  
  253. TArrowsControl::TArrowsControl()         
  254. {
  255.     fLastChange = 0;
  256. } // TArrowsControl::TArrowsControl 
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // TArrowsControl destructor
  260. //----------------------------------------------------------------------------------------
  261. #pragma segment MADestructorRes
  262.  
  263. TArrowsControl::~TArrowsControl()
  264. {
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // TArrowsControl::TrackMouse: 
  269. //----------------------------------------------------------------------------------------
  270. #pragma segment ARes
  271.  
  272. void TArrowsControl::TrackMouse(TrackPhase    aTrackPhase,
  273.                                        VPoint&        /*anchorPoint*/,
  274.                                        VPoint&        /*previousPoint*/,
  275.                                        VPoint&        nextPoint,
  276.                                        Boolean        /*mouseDidMove*/)// override 
  277. {
  278.     if (TickCount() >= fLastChange + 5)
  279.     {
  280.         fLastChange = TickCount();
  281.         if (aTrackPhase == trackBegin)
  282.             fLastChange = fLastChange + 10;
  283.  
  284.         if (ContainsMouse(nextPoint))
  285.             if (nextPoint.v <= fSize.v / 2)
  286.                 this->HandleEvent(mUpArrow, this, NULL);
  287.             else
  288.                 this->HandleEvent(mDownArrow, this, NULL);
  289.     }
  290. } // TArrowsControl::TrackMouse 
  291.  
  292.  
  293. //========================================================================================
  294. // CLASS TTemperatureCluster
  295. //========================================================================================
  296. #undef Inherited
  297. #define Inherited TCluster
  298.  
  299. #pragma segment ARes
  300. MA_DEFINE_CLASS_M1(TTemperatureCluster, Inherited);
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // TTemperatureCluster destructor
  304. //----------------------------------------------------------------------------------------
  305. #pragma segment MADestructorRes
  306.  
  307. TTemperatureCluster::~TTemperatureCluster()
  308. {
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // TTemperatureCluster::DoEvent: 
  313. //----------------------------------------------------------------------------------------
  314. #pragma segment ARes
  315.  
  316. void TTemperatureCluster::DoEvent(EventNumber    eventNumber,
  317.                                          TEventHandler*    source,
  318.                                          TEvent*    event)// override 
  319. {
  320.     TNumberText *    theNumberText;
  321.     long            theNumber;
  322.  
  323.  
  324.     switch (eventNumber)
  325.     {
  326.         case mUpArrow:
  327.         case mDownArrow:
  328.             {
  329.                 theNumberText = (TNumberText *)(FindSubView('Numb'));
  330.                 theNumber = theNumberText->GetValue();
  331.                 if (eventNumber == mUpArrow)
  332.                     theNumber = theNumber + 1;
  333.                 else
  334.                     theNumber = theNumber - 1;
  335.                 theNumberText->SetValue(theNumber, kRedraw);
  336.  
  337.                 this->HandleEvent(mTemperatureChanged, this, NULL);
  338.                 break;
  339.             }
  340.         default:
  341.             Inherited::DoEvent(eventNumber, source, event);
  342.     } //switch
  343. } // TTemperatureCluster::DoEvent 
  344.  
  345. //----------------------------------------------------------------------------------------
  346. // TTemperatureCluster::GetValue: 
  347. //----------------------------------------------------------------------------------------
  348. #pragma segment ARes
  349.  
  350. long TTemperatureCluster::GetValue()
  351. {
  352.     TNumberText * numberText;
  353.  
  354.     numberText = (TNumberText *)this->FindSubView('Numb');
  355.     return (numberText->GetValue());
  356. } // TTemperatureCluster::GetValue 
  357.  
  358. //----------------------------------------------------------------------------------------
  359. // TTemperatureCluster::SetValue: 
  360. //----------------------------------------------------------------------------------------
  361. #pragma segment ARes
  362.  
  363. void TTemperatureCluster::SetValue(long        newValue,
  364.                                           Boolean    redraw)
  365. {
  366.     TNumberText * numberText;
  367.  
  368.     numberText = (TNumberText *)this->FindSubView('Numb');
  369.     numberText->SetValue(newValue, redraw);
  370. } // TTemperatureCluster::SetValue 
  371.  
  372.  
  373. //========================================================================================
  374. // CLASS TTemperatureConversionCluster
  375. //========================================================================================
  376. #undef Inherited
  377. #define Inherited TCluster
  378.  
  379. #pragma segment ARes
  380. MA_DEFINE_CLASS_M1(TTemperatureConversionCluster, Inherited);
  381.  
  382. //----------------------------------------------------------------------------------------
  383. // TTemperatureConversionCluster destructor
  384. //----------------------------------------------------------------------------------------
  385. #pragma segment MADestructorRes
  386.  
  387. TTemperatureConversionCluster::~TTemperatureConversionCluster()
  388. {
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. // TTemperatureConversionCluster::DoEvent: 
  393. //----------------------------------------------------------------------------------------
  394. #pragma segment ARes
  395.  
  396. void TTemperatureConversionCluster::DoEvent(EventNumber        eventNumber,
  397.                                                    TEventHandler*    source,
  398.                                                    TEvent*            event)
  399. {
  400.     TTemperatureCluster * Celsius;
  401.     TTemperatureCluster * Fahrenheit;
  402.  
  403.     if (eventNumber == mTemperatureChanged)
  404.     {
  405.         Celsius = (TTemperatureCluster *)(this->FindSubView('Cels'));
  406.         Fahrenheit = (TTemperatureCluster *)(this->FindSubView('Fahr'));
  407.         if (source == Celsius)
  408.         {
  409.             Fahrenheit->SetValue(Celsius->GetValue() * 1.8 + 32, kRedraw);
  410.         }
  411.         else if (source == Fahrenheit)
  412.         {
  413.             Celsius->SetValue((Fahrenheit->GetValue() - 32) / 1.8, kRedraw);
  414.         }
  415.     }
  416.     else
  417.         Inherited::DoEvent(eventNumber, source, event);
  418. } // TTemperatureConversionCluster::DoEvent 
  419.  
  420.  
  421. //========================================================================================
  422. // CLASS TSlider
  423. //========================================================================================
  424. #undef Inherited
  425. #define Inherited TPicture
  426.  
  427. #pragma segment AOpen
  428. MA_DEFINE_CLASS_M1(TSlider, Inherited);
  429.  
  430. //----------------------------------------------------------------------------------------
  431. // TSlider constructor 
  432. //----------------------------------------------------------------------------------------
  433. #pragma segment AOpen
  434.  
  435. TSlider::TSlider()                
  436. {
  437.     fValue = 0;
  438.     fKnobPicture = NULL;
  439.     fKnobRect = CRect(0, 0, 0, 0);
  440.     fMinTop = 0;
  441.     fMaxTop = 0;
  442. } // TSlider::TSlider 
  443.  
  444. //----------------------------------------------------------------------------------------
  445. // TSlider destructor
  446. //----------------------------------------------------------------------------------------
  447. #pragma segment MADestructorRes
  448.  
  449. TSlider::~TSlider()
  450. {
  451. }
  452.  
  453. //----------------------------------------------------------------------------------------
  454. // TSlider::DoPostCreate: 
  455. //----------------------------------------------------------------------------------------
  456. #pragma segment AOpen
  457.  
  458. void TSlider::DoPostCreate(TDocument* itsDocument)// override 
  459. {
  460.     PicHandle    knobPicture;
  461.     VRect        knobRect;
  462.     CRect        tmpR;
  463.  
  464.  
  465.     Inherited::DoPostCreate(itsDocument);
  466.  
  467.     knobPicture = GetPicture(1002);
  468.     fKnobPicture = knobPicture;
  469.     tmpR = (*knobPicture)->picFrame;
  470.     knobRect.left = fSize.h - (tmpR.right - tmpR.left);
  471.     knobRect.top = fSize.v - 16;
  472.     knobRect.right = knobRect.left + tmpR.right - tmpR.left;
  473.     knobRect.bottom = knobRect.top + tmpR.bottom - tmpR.top;
  474.  
  475.     fKnobRect = knobRect.ToRect();
  476.     fMinTop = fKnobRect.top - 84;
  477.     fMaxTop = fKnobRect.top;
  478. } // TSlider::DoPostCreate 
  479.  
  480. //----------------------------------------------------------------------------------------
  481. // TSlider::ContainsMouse: 
  482. //----------------------------------------------------------------------------------------
  483. #pragma segment ARes
  484.  
  485. Boolean TSlider::ContainsMouse(const VPoint& theMouse)// override 
  486. {
  487.     CRect knobRect;
  488.  
  489.     GetKnobRect(knobRect);
  490.     return PtInRect(VPtToPt(theMouse), knobRect);
  491. } // TSlider::ContainsMouse 
  492.  
  493. //----------------------------------------------------------------------------------------
  494. // TSlider::Draw: 
  495. //----------------------------------------------------------------------------------------
  496. #pragma segment ARes
  497.  
  498. void TSlider::Draw(const VRect& area)// override
  499. {
  500.     Inherited::Draw(area);                        // This draws everything but the knob
  501.     DrawKnob();
  502. } // TSlider::Draw 
  503.  
  504. //----------------------------------------------------------------------------------------
  505. // TSlider::DrawKnob: 
  506. //----------------------------------------------------------------------------------------
  507. #pragma segment ARes
  508.  
  509. void TSlider::DrawKnob()
  510. {
  511.     CRect knobRect;
  512.  
  513.     GetKnobRect(knobRect);
  514.     LoadResource(Handle(fKnobPicture));            // In case it was purged from memory 
  515.     DrawPicture(fKnobPicture, knobRect);
  516. } // TSlider::DrawKnob 
  517.  
  518. //----------------------------------------------------------------------------------------
  519. // TSlider::GetKnobRect: 
  520. //----------------------------------------------------------------------------------------
  521. #pragma segment ARes
  522.  
  523. void TSlider::GetKnobRect(CRect& knobRect)
  524. {
  525.     knobRect = fKnobRect;
  526.     OffsetRect(knobRect, 0, -(fValue * 12));
  527. } // TSlider::GetKnobRect 
  528.  
  529. //----------------------------------------------------------------------------------------
  530. // TSlider::TrackMouse: 
  531. //----------------------------------------------------------------------------------------
  532. #pragma segment ARes
  533.  
  534. void TSlider::TrackMouse(TrackPhase    aTrackPhase,
  535.                                 VPoint&        anchorPoint,
  536.                                 VPoint&        previousPoint,
  537.                                 VPoint&        nextPoint,
  538.                                 Boolean        /*mouseDidMove*/)// override 
  539. {
  540.     CRect        oldRect;
  541.     CRect        newRect;
  542.     CRect        difference;
  543.     RgnHandle    savedClip;
  544.     short        base;
  545.     short        offset;
  546.  
  547.  
  548.     GetKnobRect(oldRect);
  549.     newRect = oldRect;
  550.  
  551.     // Compute rectangle of knob's current position 
  552.     offset = (short)Min(Max(previousPoint.v - anchorPoint.v, fMinTop - newRect.top), fMaxTop - newRect.top);
  553.     OffsetRect(oldRect, 0, offset);
  554.  
  555.     // Compute rectangle of knob's new position 
  556.     offset = (short)Min(Max(nextPoint.v - anchorPoint.v, fMinTop - newRect.top), fMaxTop - newRect.top);
  557.     OffsetRect(newRect, 0, offset);
  558.  
  559.     // If the mouse was released, pin the knob to a value and set slider's value 
  560.     if (aTrackPhase == trackEnd)
  561.     {
  562.         base = ((short)(fSize.v - 16 - newRect.top));
  563.         offset = base - ((base + 6) / 12 * 12);
  564.         OffsetRect(newRect, 0, offset);
  565.         fValue = ((short)(fSize.v - 16 - newRect.top) / 12);
  566.     }
  567.  
  568.     // To redraw minimal amount, compute difference between old and new positions 
  569.     difference = oldRect;
  570.     if (oldRect.top > newRect.top)
  571.         difference.top = Max(oldRect.top, newRect.bottom);
  572.     else
  573.         difference.bottom = Min(oldRect.bottom, newRect.top);
  574.  
  575.     savedClip = MakeNewRgn();
  576.     GetClip(savedClip);
  577.  
  578.     ClipRect(difference);
  579.     Inherited::Draw(QDToViewRect(difference));
  580.  
  581.     SetClip(savedClip);
  582.     DisposeRgn(savedClip);
  583.  
  584.     LoadResource(Handle(fKnobPicture));            // In case it was purged from memory 
  585.     DrawPicture(fKnobPicture, newRect);
  586. } // TSlider::TrackMouse 
  587.  
  588.  
  589.  
  590. //========================================================================================
  591. // CLASS TSumStaticText
  592. //========================================================================================
  593. #undef Inherited
  594. #define Inherited TStaticText
  595.  
  596. #pragma segment ARes
  597. MA_DEFINE_CLASS_M1(TSumStaticText, Inherited);
  598.  
  599. //----------------------------------------------------------------------------------------
  600. // TSumStaticText destructor
  601. //----------------------------------------------------------------------------------------
  602. #pragma segment MADestructorRes
  603.  
  604. TSumStaticText::~TSumStaticText()
  605. {
  606. }
  607.  
  608. //----------------------------------------------------------------------------------------
  609. // TSumStaticText::Draw: 
  610. //----------------------------------------------------------------------------------------
  611. #pragma segment ARes
  612.  
  613. void TSumStaticText::Draw(const VRect& area)// override 
  614.  
  615. {
  616.     // Make sure the gray background gets completely erased 
  617.     //InsetRect(theRect, fPenSize.h, fPenSize.v);
  618.     EraseRect(&GetQDExtent());
  619.  
  620.     Inherited::Draw(area);
  621. } // TSumStaticText::Draw 
  622.  
  623.  
  624. //========================================================================================
  625. // CLASS TCalcDialog
  626. //========================================================================================
  627. #undef Inherited
  628. #define Inherited TPicture
  629.  
  630. #pragma segment ARes
  631. MA_DEFINE_CLASS_M1(TCalcDialog, Inherited);
  632.  
  633. //----------------------------------------------------------------------------------------
  634. // TCalcDialog destructor
  635. //----------------------------------------------------------------------------------------
  636. #pragma segment MADestructorRes
  637.  
  638. TCalcDialog::~TCalcDialog()
  639. {
  640. }
  641.  
  642. //----------------------------------------------------------------------------------------
  643. // TCalcDialog::DoEvent: 
  644. //----------------------------------------------------------------------------------------
  645. #pragma segment ARes
  646.  
  647. void TCalcDialog::DoEvent(EventNumber    eventNumber,
  648.                                  TEventHandler*    source,
  649.                                  TEvent*        event)
  650. {
  651.     IDType    origID;
  652.     char    keyHit;
  653.     
  654.     // dcs - 3.0 fix : Bad News. its either this or a mess of FindSubView calls..
  655.     // Can we count on source being a view at this call.. Hmmm..
  656.     //    origID = source->fIdentifier;
  657.     origID = ((TView *)source)->fIdentifier;
  658.     keyHit = (char) (origID & 0xFF);    // strip all but the last letter of the ID, which
  659.                                         // we've assigned to the name of the key
  660.     switch (eventNumber)
  661.     {
  662.         case mOKHit:                            /*??? Should be mControlHit*/
  663.             switch (keyHit)
  664.             {
  665.                 case 'C':
  666.                     ClearSum();
  667.                     break;
  668.                 case '=':
  669.                 case 'E':
  670.                     TotalSum(noOperator);
  671.                     break;
  672.                 case '/':
  673.                     TotalSum(divOperator);
  674.                     break;
  675.                 case '*':
  676.                     TotalSum(mulOperator);
  677.                     break;
  678.                 case '-':
  679.                     TotalSum(subOperator);
  680.                     break;
  681.                 case '+':
  682.                     TotalSum(addOperator);
  683.                     break;
  684.                 default:
  685.                     NewDigit(keyHit);
  686.                     break;
  687.             }
  688.             break;
  689.  
  690.         default:
  691.             Inherited::DoEvent(eventNumber, source, event);
  692.             break;
  693.     }
  694. } // TCalcDialog::DoEvent 
  695.  
  696.  
  697. //----------------------------------------------------------------------------------------
  698. // TCalcDialog::DoKeyEvent: 
  699. //----------------------------------------------------------------------------------------
  700. #pragma segment ARes
  701.  
  702. void TCalcDialog::DoKeyEvent(TToolboxEvent* event)// override 
  703. {
  704.     Boolean invalidKeyStroke = FALSE;
  705.  
  706.     // Determine the ID to find the view which will be used as the originator of the keystroke 
  707.     IDType    theID;
  708.     if (event->fKeyCode == kClearVirtualCode)
  709.         theID = 'keyC';
  710.     else
  711.     {
  712.         if (event->fText == '0'
  713.         || event->fText == '1'
  714.         || event->fText == '2'
  715.         || event->fText == '3'
  716.         || event->fText == '4'
  717.         || event->fText == '5'
  718.         || event->fText == '6'
  719.         || event->fText == '7'
  720.         || event->fText == '8'
  721.         || event->fText == '9'
  722.         || event->fText == '.'
  723.         || event->fText == '+'
  724.         || event->fText == '*'
  725.         || event->fText == '/'
  726.         || event->fText == '='
  727.         || event->fText == '1')
  728.             theID = 'key ' | event->fText[1];
  729.         else if (event->fText == chEnter || event->fText == chReturn)
  730.             theID = 'keyE';
  731.         else
  732.             invalidKeyStroke = TRUE;
  733.     }
  734.  
  735.     if (!invalidKeyStroke)
  736.     {
  737.         TView *    theView = FindSubView(theID);
  738.         if (theView != NULL)
  739.             ((TControl *)(theView))->Flash();
  740.         this->HandleEvent(mOKHit, theView, NULL);
  741.     }
  742.     else
  743.         SysBeep(1);
  744. } // TCalcDialog::DoKeyEvent 
  745.  
  746. //----------------------------------------------------------------------------------------
  747. // TCalcDialog::ClearSum: 
  748. //----------------------------------------------------------------------------------------
  749. #pragma segment ARes
  750.  
  751. void TCalcDialog::ClearSum()
  752. {
  753.     fArgument = 0;
  754.     fSum = 0;
  755.     fOperator = noOperator;
  756.     fDecimalPoint = FALSE;
  757.     fRestart = TRUE;
  758.     ((TStaticText *)(FindSubView('SUMM')))->SetText("0", TRUE);
  759. } // TCalcDialog::ClearSum 
  760.  
  761. //----------------------------------------------------------------------------------------
  762. // TCalcDialog::FetchValue: 
  763. //----------------------------------------------------------------------------------------
  764. #pragma segment ARes
  765.  
  766. double TCalcDialog::FetchValue()
  767. {
  768.     CStr255 theStr;
  769.  
  770.     ((TStaticText *)FindSubView('SUMM'))->GetText(theStr);
  771.     return atof((const char*)theStr);
  772. } // TCalcDialog::FetchValue 
  773.  
  774. //----------------------------------------------------------------------------------------
  775. // TCalcDialog::SetValue: 
  776. //----------------------------------------------------------------------------------------
  777. #pragma segment ARes
  778.  
  779. void TCalcDialog::SetValue()
  780. {
  781.     CStr255        theStr;
  782.     double        aDouble;
  783.     char        aCString[255];
  784.     double        orderOfMagnitude;
  785.  
  786.  
  787.     aDouble = fSum;
  788.     orderOfMagnitude = log2(fabs(aDouble)) / log2(10);        // this says how many digits are 
  789.                                                             // left of the decimal point
  790.     if ((orderOfMagnitude > kMaxPlaces - 1) || (orderOfMagnitude < -6))
  791.     {
  792.         short mantissaLength;
  793.         
  794.         orderOfMagnitude = log2(fabs(orderOfMagnitude)) / log2(10);// find the order of the exponent
  795.         if (orderOfMagnitude > 2)
  796.             mantissaLength = kMaxPlaces - 9;
  797.         else
  798.             mantissaLength = kMaxPlaces - 7;
  799.             
  800.         sprintf(aCString, "%.*e", mantissaLength, aDouble);    // use floating point conversion
  801.         theStr = aCString;
  802.     }
  803.     else
  804.     {
  805.         sprintf(aCString, "%.*f", kMaxPlaces - 1, aDouble);
  806.         theStr = aCString;
  807.         this->Truncate(theStr);                    // strip insignificant zeros / decimal point
  808.  
  809.         // now we must make sure it fits in the space in the dialog, which we know to be
  810.         // kMaxPlaces wide (aren't fixed width fonts great?).  If the number is still too
  811.         // large, we can just chop off what's off the end, because we know that the most
  812.         // significant part of the number will still be correctly represented.
  813.         if (theStr.Length() > kMaxPlaces)        
  814.             theStr.Delete(kMaxPlaces, theStr.Length() - kMaxPlaces);
  815.     }
  816.     ((TStaticText *)(FindSubView('SUMM')))->SetText(theStr, TRUE);
  817. } // TCalcDialog::SetValue 
  818.  
  819. //----------------------------------------------------------------------------------------
  820. // TCalcDialog::Truncate: 
  821. //----------------------------------------------------------------------------------------
  822. void TCalcDialog::Truncate(CStr255& theStr)
  823. {
  824.     short thePos;
  825.  
  826.     thePos = theStr.Length();
  827.     while (theStr[thePos] == '0')
  828.         --thePos;                                // strip off 0s right of the decimal point
  829.     if (theStr[thePos] == '.')                    // there is no fractional part so
  830.         --thePos;                                // also strip the decimal point.
  831.  
  832.     if (thePos != theStr.Length())                // if we've decided to cut something
  833.     {
  834.         if (thePos == 0)
  835.         {
  836.             theStr = "0";        // if everything has been stripped, we know the value is 0.
  837.         }
  838.         else
  839.         {
  840.             theStr.Delete(thePos + 1, theStr.Length() - thePos); // cut it.
  841.         }
  842.     }
  843. } // TCalcDialog::Truncate 
  844.  
  845. //----------------------------------------------------------------------------------------
  846. // TCalcDialog::TotalSum: 
  847. //----------------------------------------------------------------------------------------
  848. #pragma segment ARes
  849.  
  850. void TCalcDialog::TotalSum(CalcOperator newOperator)
  851. {
  852.     fArgument = FetchValue();
  853.     switch (fOperator)
  854.     {
  855.         case noOperator:
  856.             fSum = fArgument;
  857.             break;
  858.         case divOperator:
  859.             fSum = fSum / fArgument;
  860.             break;
  861.         case mulOperator:
  862.             fSum = fSum * fArgument;
  863.             break;
  864.         case subOperator:
  865.             fSum = fSum - fArgument;
  866.             break;
  867.         case addOperator:
  868.             fSum = fSum + fArgument;
  869.             break;
  870.     }
  871.     SetValue();
  872.     fRestart = TRUE;
  873.     fArgument = fSum;
  874.     fOperator = newOperator;
  875. } // TCalcDialog::TotalSum 
  876.  
  877. //----------------------------------------------------------------------------------------
  878. // TCalcDialog::NewDigit: 
  879. //----------------------------------------------------------------------------------------
  880. #pragma segment ARes
  881.  
  882. void TCalcDialog::NewDigit(char theNumber)
  883.  
  884. {
  885.     TStaticText * theText = (TStaticText *)(FindSubView('SUMM'));
  886.     CStr255 theStr;
  887.     if (!fRestart)
  888.         theText->GetText(theStr);
  889.  
  890.     if (theStr.Length() < kMaxPlaces - 1)    // is there room for another digit? 
  891.     {
  892.         fRestart = FALSE;
  893.         if ((theNumber == '.') &&!fDecimalPoint)
  894.             fDecimalPoint = TRUE;
  895.         theStr += theNumber;    // append the char onto the end
  896.         theText->SetText(theStr, TRUE);
  897.     }
  898. } // TCalcDialog::NewDigit 
  899.  
  900.  
  901. //========================================================================================
  902. // CLASS TAdornmentDemo
  903. //========================================================================================
  904. #undef Inherited
  905. #define Inherited TView
  906.  
  907. #pragma segment ARes
  908. MA_DEFINE_CLASS_M1(TAdornmentDemo, Inherited);
  909.  
  910. //----------------------------------------------------------------------------------------
  911. // TAdornmentDemo constructor 
  912. //----------------------------------------------------------------------------------------
  913. #pragma segment ARes
  914.  
  915. TAdornmentDemo::TAdornmentDemo()     
  916. {
  917.     fTestView = NULL;
  918. } // TAdornmentDemo::Initialize 
  919.  
  920. //----------------------------------------------------------------------------------------
  921. // TAdornmentDemo destructor
  922. //----------------------------------------------------------------------------------------
  923. #pragma segment MADestructorRes
  924.  
  925. TAdornmentDemo::~TAdornmentDemo()
  926. {
  927. }
  928.  
  929. //----------------------------------------------------------------------------------------
  930. // TAdornmentDemo::DoPostCreate: 
  931. //----------------------------------------------------------------------------------------
  932. #pragma segment ARes
  933.  
  934. void TAdornmentDemo::DoPostCreate(TDocument* itsDocument)
  935. {
  936.     Inherited::DoPostCreate(itsDocument);
  937.  
  938.     fTestView = this->FindSubView('test');
  939. } // TAdornmentDemo::DoPostCreate 
  940.  
  941. //----------------------------------------------------------------------------------------
  942. // TAdornmentDemo::DoEvent: 
  943. //----------------------------------------------------------------------------------------
  944. #pragma segment ARes
  945.  
  946. void TAdornmentDemo::DoEvent(EventNumber        eventNumber,
  947.                                     TEventHandler*    source,
  948.                                     TEvent*            event)// override 
  949. {
  950.     TView *            whichView;
  951.     AdornPriority    priority;
  952.  
  953.     // Each adorner has the same class signature and ID as the ID of the corresponding checkbox 
  954.     if (eventNumber == mCheckBoxHit)
  955.     {
  956.  
  957.         if (source->fIdentifier == 'gray')
  958.         {
  959.             priority = kAdornFirst;
  960.             whichView = this;
  961.         }
  962.         else if (source->fIdentifier == 'fram')
  963.         {
  964.             priority = kAdornLast;                // lower than the embossed frame adorner 
  965.             whichView = fTestView;
  966.         }
  967.         else
  968.         {
  969.             priority = kAdornAfter;
  970.             whichView = fTestView;
  971.         }
  972.  
  973.         if (((TCheckBox *)(source))->IsOn())
  974.             whichView->AddAdorner(NewStdAdorner(source->fIdentifier, gEmptyString, source->fIdentifier, kFreeOnDeletion), priority, kRedraw);
  975.         else
  976.             whichView->DeleteAdornerByID(source->fIdentifier, kRedraw);
  977.  
  978.     }
  979.     else
  980.         Inherited::DoEvent(eventNumber, source, event);
  981. } // TAdornmentDemo::DoEvent 
  982.  
  983.  
  984. //========================================================================================
  985. // CLASS TEmbossedFrame
  986. //========================================================================================
  987. #undef Inherited
  988. #define Inherited TAdorner
  989.  
  990. #pragma segment ARes
  991. MA_DEFINE_CLASS_M1(TEmbossedFrame, Inherited);
  992.  
  993. //----------------------------------------------------------------------------------------
  994. // TEmbossedFrame destructor
  995. //----------------------------------------------------------------------------------------
  996. #pragma segment MADestructorRes
  997.  
  998. TEmbossedFrame::~TEmbossedFrame()
  999. {
  1000. }
  1001.  
  1002. //----------------------------------------------------------------------------------------
  1003. // TEmbossedFrame::Draw: 
  1004. //----------------------------------------------------------------------------------------
  1005. #pragma segment ARes
  1006.  
  1007. void TEmbossedFrame::Draw(TView*            itsView,
  1008.                                  const VRect&    /*area*/)// override 
  1009. {
  1010.     CRGBColor    saveColor;
  1011.     VRect        adornArea;
  1012.     CRect        tempRect;
  1013.     CRGBColor    anRGBGray;
  1014.     RgnHandle    outerRgn;
  1015.     RgnHandle    innerRgn;
  1016.  
  1017.  
  1018.     PenNormal();
  1019.  
  1020.     itsView->GetAdornExtent(adornArea);
  1021.     CRect QDArea(itsView->ViewToQDRect(adornArea));
  1022.     tempRect = QDArea;
  1023.  
  1024.     // • Build the region to use for filling, first create the outer region
  1025.     InsetRect(QDArea, 1, 1);
  1026.     outerRgn = NewRgn();
  1027.     RectRgn(outerRgn, QDArea);
  1028.  
  1029.     // • Create the inner region
  1030.     InsetRect(QDArea, 6, 6);
  1031.     innerRgn = NewRgn();
  1032.     RectRgn(innerRgn, QDArea);
  1033.  
  1034.     // • Finally subtract the inner from the outer region
  1035.     DiffRgn(outerRgn, innerRgn, outerRgn);
  1036.  
  1037.     // • Get rid of the inner region
  1038.     DisposeRgn(innerRgn);
  1039.  
  1040.     // • Set the foreground color to the global face color, this allows the user to
  1041.     //     change the color of the face                                                                                                        
  1042.     SetIfColor(gRGBLtGray);
  1043.     PaintRgn(outerRgn);
  1044.  
  1045.     // • Get rid of the outer region
  1046.     DisposeRgn(outerRgn);
  1047.  
  1048.     // • Draw the inside black frame
  1049.     SetIfColor(gRGBBlack);
  1050.     InsetRect(QDArea, 1, 1);
  1051.     FrameRect(&QDArea);
  1052.  
  1053.     // • Draw the shadows
  1054.     // THIS WAS "WITH tempRect"
  1055.     {
  1056.         // • Draw the outer gray shadow
  1057.         SetIfColor(gRGBGray);
  1058.         MoveTo(tempRect.left + 1, tempRect.bottom - 1);        //(h, v)
  1059.         LineToPt(tempRect[botRight] - CPoint(1, 1));    //(h, v)
  1060.         LineTo(tempRect.right - 1, tempRect.top + 1);        //(h, v)
  1061.  
  1062.         // • Draw the inner gray shadow    
  1063.         SetIfColor(gRGBGray);
  1064.         MoveTo(tempRect.left + 7, tempRect.bottom - 8);        //(h, v)
  1065.         LineToPt(tempRect[topLeft] + CPoint(7, 7));        //(h, v)
  1066.         LineTo(tempRect.right - 8, tempRect.top + 7);        //(h, v)
  1067.     }
  1068.  
  1069.     // • Restore the drawing environment
  1070.     itsView->SetupDrawingEnvironment();
  1071. } // TEmbossedFrame::Draw 
  1072.  
  1073.  
  1074. //========================================================================================
  1075. // CLASS TGrayFill
  1076. //========================================================================================
  1077. #undef Inherited
  1078. #define Inherited TAdorner
  1079.  
  1080. #pragma segment ARes
  1081. MA_DEFINE_CLASS_M1(TGrayFill, Inherited);
  1082.  
  1083. //----------------------------------------------------------------------------------------
  1084. // TGrayFill destructor
  1085. //----------------------------------------------------------------------------------------
  1086. #pragma segment MADestructorRes
  1087.  
  1088. TGrayFill::~TGrayFill()
  1089. {
  1090. }
  1091.  
  1092. //----------------------------------------------------------------------------------------
  1093. // TGrayFill::Draw: 
  1094. //----------------------------------------------------------------------------------------
  1095. #pragma segment ARes
  1096.  
  1097. void TGrayFill::Draw(TView*            itsView,
  1098.                             const VRect&    /*area*/)// override 
  1099. {
  1100.     CRGBColor    saveColor;
  1101.     PenState    savePenState;
  1102.     VRect        adornArea;
  1103.     CRect        QDArea;
  1104.  
  1105.  
  1106.     // • Save off the current pen state and the foreground color
  1107.     GetPenState(&savePenState);
  1108.     GetIfColor(saveColor);
  1109.     PenNormal();
  1110.  
  1111.     itsView->GetAdornExtent(adornArea);
  1112.     itsView->ViewToQDRect(adornArea, QDArea);
  1113.  
  1114.     SetIfColor(gRGBGray);
  1115.     PaintRect(QDArea);
  1116.  
  1117.     // • Restore the foreground color and the pen
  1118.     SetIfColor(saveColor);
  1119.     SetPenState(&savePenState);
  1120. } // TGrayFill::Draw 
  1121.  
  1122.  
  1123. //========================================================================================
  1124. // CLASS TEtchedFrame
  1125. //========================================================================================
  1126. #undef Inherited
  1127. #define Inherited TAdorner
  1128.  
  1129. #pragma segment ARes
  1130. MA_DEFINE_CLASS_M1(TEtchedFrame, Inherited);
  1131.  
  1132. //----------------------------------------------------------------------------------------
  1133. // TEtchedFrame constructor 
  1134. //----------------------------------------------------------------------------------------
  1135. #pragma segment ARes
  1136.  
  1137. TEtchedFrame::TEtchedFrame() 
  1138. {
  1139.     fInset.h = 1;
  1140.     fInset.v = 1;
  1141. } // TEtchedFrame::TEtchedFrame 
  1142.  
  1143. //----------------------------------------------------------------------------------------
  1144. // TEtchedFrame destructor
  1145. //----------------------------------------------------------------------------------------
  1146. #pragma segment MADestructorRes
  1147.  
  1148. TEtchedFrame::~TEtchedFrame()
  1149. {
  1150. }
  1151.  
  1152. //----------------------------------------------------------------------------------------
  1153. // TEtchedFrame::Draw: 
  1154. //----------------------------------------------------------------------------------------
  1155. #pragma segment ARes
  1156.  
  1157. void TEtchedFrame::Draw(TView*            itsView,
  1158.                                const VRect&        /*area*/)    // override 
  1159. {
  1160.     CRGBColor    saveColor;
  1161.     PenState    savePenState;
  1162.     VRect         adornArea;
  1163.  
  1164.  
  1165.     // • Save off the current pen state and the foreground color
  1166.     GetPenState(&savePenState);
  1167.     GetIfColor(saveColor);
  1168.     PenNormal();
  1169.  
  1170.     itsView->GetAdornExtent(adornArea);
  1171.     CRect QDArea(itsView->ViewToQDRect(adornArea));
  1172.  
  1173.     SetIfColor(gRGBBlack);
  1174.     InsetRect(QDArea, fInset.h, fInset.v);
  1175.     FrameRect(QDArea);                                    // Draw the shadows 
  1176.  
  1177.     OffsetRect(QDArea, 1, 1);
  1178.     SetIfColor(gRGBWhite);
  1179.     FrameRect(QDArea);                                    // Draw the light edges 
  1180.  
  1181.     OffsetRect(QDArea, -1, -1);
  1182.     SetCPixel(QDArea.left, QDArea.bottom, gRGBBlack);    // Finish the corners 
  1183.     SetCPixel(QDArea.right, QDArea.top, gRGBBlack);
  1184.  
  1185.     // • Restore the foreground color and the pen
  1186.     SetIfColor(saveColor);
  1187.     SetPenState(&savePenState);
  1188. } // TEtchedFrame::Draw 
  1189.  
  1190.  
  1191. //========================================================================================
  1192. // CLASS TSlowScroller
  1193. //========================================================================================
  1194. #undef Inherited
  1195. #define Inherited TScroller
  1196.  
  1197. #pragma segment ARes
  1198. MA_DEFINE_CLASS_M1(TSlowScroller, Inherited);
  1199.  
  1200. //----------------------------------------------------------------------------------------
  1201. // TSlowScroller constructor 
  1202. //----------------------------------------------------------------------------------------
  1203. #pragma segment ARes
  1204.  
  1205. TSlowScroller::TSlowScroller() 
  1206. {
  1207. } // TSlowScroller::TSlowScroller 
  1208.  
  1209. //----------------------------------------------------------------------------------------
  1210. // TSlowScroller destructor
  1211. //----------------------------------------------------------------------------------------
  1212. #pragma segment MADestructorRes
  1213.  
  1214. TSlowScroller::~TSlowScroller()
  1215. {
  1216. }
  1217.  
  1218. //----------------------------------------------------------------------------------------
  1219. // TSlowScroller::ScrollStep: 
  1220. //----------------------------------------------------------------------------------------
  1221. #pragma segment ARes
  1222.  
  1223. VCoordinate TSlowScroller::ScrollStep(VHSelect vhs, short partCode) // override
  1224. {
  1225.     VCoordinate scrollUnit = fScrollUnit[vhs];
  1226.     if (IsControlKeyDown())
  1227.         scrollUnit = 1;
  1228.     VCoordinate delta;
  1229.  
  1230.     switch (partCode)
  1231.     {
  1232.         case kControlUpButtonPart:
  1233.         case kControlDownButtonPart:
  1234.             delta = scrollUnit;
  1235.             break;
  1236.             
  1237.         case kControlPageUpPart:
  1238.         case kControlPageDownPart:
  1239.             if (IsControlKeyDown())
  1240.                 delta = 1;
  1241.             else
  1242.                 delta = fSize[vhs] - scrollUnit;
  1243.             break;
  1244.             
  1245. #if qDebug
  1246.         default:
  1247.             {
  1248.                 CStr255 theString;
  1249.                 ConcatNumber("TScroller::ScrollStep: bad part code =", partCode, theString);
  1250.                 ProgramBreak(theString);
  1251.             }
  1252.             break;
  1253. #endif
  1254.  
  1255.     }
  1256.  
  1257.     if ((partCode == kControlUpButtonPart) || (partCode == kControlPageUpPart))
  1258.         delta = -delta;
  1259.  
  1260.     // Constrain if necessary 
  1261.     if (fConstrain[vhs] && (scrollUnit != 0))
  1262.     {
  1263.         VCoordinate adjustment = (Max(0, fTranslation[vhs] + delta)) % scrollUnit;
  1264.         if (adjustment != 0)
  1265.             if (delta > 0)
  1266.                 delta -= adjustment;
  1267.             else
  1268.                 delta += (scrollUnit - adjustment);
  1269.     }
  1270.  
  1271.     VPoint deltaPt(0, 0);
  1272.     deltaPt[vhs] = delta;
  1273.  
  1274.     this->DoScroll(deltaPt, kRedraw);
  1275.     return delta;
  1276. } // TSlowScroller::Draw 
  1277.  
  1278. //----------------------------------------------------------------------------------------
  1279. // End of UDemoDialogs.More.cp
  1280.  
  1281. #pragma segment Inline
  1282.